﻿using System.Text;
using BitTorrent.Bencode;
using BitTorrent.Bencode.Exceptions;
using MbUnit.Framework;

namespace UnitTests.Bencode {
    /// <summary>
    /// Summary description for ByteStringTests
    /// </summary>
    [TestFixture]
    public class ByteStringTests {
        [Test]
        public void DecodeByteStringOk() {
            Object actual = BencodeFactory.Decode(Encoding.UTF8.GetBytes("3:foo"));
            Assert.IsTrue(actual is ByteString);
            Assert.IsTrue(((ByteString) actual).String == "foo");
        }

        [Test]
        [ExpectedException(typeof (BencodeException))]
        public void DecodeByteStringGoingPastEnd() {
            Object actual = BencodeFactory.Decode(Encoding.UTF8.GetBytes("42:foobar"));
        }

        [Test]
        public void DecodeByteStringEmptyString() {
            Object actual = BencodeFactory.Decode(Encoding.UTF8.GetBytes(""));
            Assert.IsNull(actual);
        }

        [Test]
        public void DecodeByteStringShortString() {
            Object actual = BencodeFactory.Decode(Encoding.UTF8.GetBytes("2:foo"));
            Assert.IsTrue(actual is ByteString);
            Assert.IsTrue(((ByteString) actual).String == "fo");
        }

        [Test]
        public void DecodeByteStringEmptyStringV2() {
            Object actual = BencodeFactory.Decode(Encoding.UTF8.GetBytes("0:"));
            Assert.IsTrue(actual is ByteString);
            Assert.IsTrue(((ByteString) actual).String == string.Empty);
        }
    }
}